'use strict';
const BaseService = require('./base');



class RedisService extends BaseService {


    async set(key, val, ex) {
        const {redis} = this.app;
        let value = val;
        if (!ex) {
            await redis.set(key, value);
        } else {
            await redis.set(key, value, 'EX', ex);
        }
        return 1;
    }

    async get(key) {
        const {redis} = this.app;
        let data = await redis.get(key);
        try {
            if (data) {
                data = JSON.parse(data);
                return data;
            } else {
                return data;
            }
        } catch (e) {
            return data;
        }
    }

    async del(key) {
        const {redis} = this.app;
        let data = await redis.del(key);
        return data;
    }

    async setList(key, list) {
        const {redis} = this.app;
        let data = await redis.lpush(key, list);
        return data;
    }

    async getList(key) {
        const {redis} = this.app;
        let data = await redis.lrange(key, 0, -1);
        return data;
    }

}

module.exports = RedisService;
